/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.session;

import com.thienloc.omap.entity.TranDau;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.slf4j.LoggerFactory;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author GiangLT
 */
@Stateless
public class TranDauFacade extends AbstractFacade<TranDau> {

    @PersistenceContext(unitName = "omapPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public TranDauFacade() {
        super(TranDau.class);
    }

    public List<TranDau> findLiveMatches() {
        List<TranDau> result = new ArrayList<TranDau>();
        try {
            TypedQuery<TranDau> q = em.createQuery("SELECT b FROM TranDau b WHERE b.trangThai='Playing'", TranDau.class);
//            q.setParameter("time", new Date());
            result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(this.getClass()).error("", ex);
        }
        return result;
    }

    public List<TranDau> findCommingMatches() {
        List<TranDau> result = new ArrayList<TranDau>();
        try {
            TypedQuery<TranDau> q = em.createQuery("SELECT b FROM TranDau b WHERE b.trangThai='Comming' AND b.gioThiDau > :time", TranDau.class);
            q.setParameter("time", new Date());
            result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(this.getClass()).error("", ex);
        }
        return result;
    }

    public List<TranDau> findPlayedMatches() {
        List<TranDau> result = new ArrayList<TranDau>();
        try {
            TypedQuery<TranDau> q = em.createQuery("SELECT b FROM TranDau b WHERE b.trangThai='FT' AND b.gioCapNhatCuoi >=:time", TranDau.class);
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            Date d = calendar.getTime();
            q.setParameter("time", d);
            result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(this.getClass()).error("", ex);
        }
        return result;
    }

    public void update2PlayedMatch(int id) {
        try {
            TranDau entity = em.find(TranDau.class, id);
            entity.setTrangThai("FT");
            entity.setPhutThiDau("");
            em.merge(entity);
        } catch (Exception ex) {
            LoggerFactory.getLogger(this.getClass()).error("", ex);
        }
    }
}
